var _dec, _class, _class2, _temp;

import React from 'react';
import { Component } from "@tarojs/taro-rn";
import { View, Text, Image, RichText } from "@tarojs/components-rn";
import { AtToast } from "taro-ui";

import { getRuntime } from "../../utils/common";
import { toJS } from "mobx";
import { inject, observer } from "@tarojs/mobx-rn";

import notice_detailStyleSheet from "./notice_detail_styles";
import iconCollection from '../../assets/img/icon_03.png';
import iconCollection_on from '../../assets/img/icon_03_on.png';
import iconShare from '../../assets/img/icon_04.png';

var _styleSheet = notice_detailStyleSheet;
let Detail = (_dec = inject("stores"), _dec(_class = observer(_class = (_temp = _class2 = class Detail extends Component {

  constructor(props) {
    super(props);
    this.state = {
      item: {},
      isCollection: false,
      isTips: false,
      tipsText: ''
    };
  }

  componentWillMount() {
    this.getDynamicInfo(this.$router.params.id);
  }

  toggleCollection() {
    let { userStore } = this.props.stores;
    this.setState({
      isCollection: !this.state.isCollection
    }, () => {
      console.log(this.state.isCollection);
      if (this.state.isCollection) {
        // 收藏
        userStore.saveUserCollection({
          form: {
            dynamicsId: parseInt(this.$router.params.id),
            userCollectionType: 0
          },
          callback: () => {
            this.setState({ isTips: true, tipsText: '收藏成功~' });
          }
        });
      } else {
        // 取消收藏
        userStore.cancelUserCollection({
          form: {
            id: parseInt(this.$router.params.id),
            userCollectionType: 0
          },
          callback: () => {
            this.setState({ isTips: true, tipsText: '取消收藏成功~' });
          }
        });
      }
    });
  }

  getDynamicInfo(id) {
    let dynamicStore = this.props.stores.DynamicStore;
    dynamicStore.getDynamicInfo({
      id,
      callback: res => {
        // console.log(res)
        this.setState({
          item: toJS(res),
          isCollection: toJS(res.isCollection)
        });
      }
    });
  }

  handelRouter(url = '/pages/index/index') {
    getRuntime().navigateTo(url);
  }

  render() {
    let { item, isCollection, isTips, tipsText } = this.state;
    return <View style={_styleSheet["article-wrap"]}>
       <View style={_styleSheet["article-info"]}>
         <View style={_styleSheet["title"]}>
           {item.title}
         </View>
         <Text style={_styleSheet["source"]}>{item.author}</Text>
         <Text style={_styleSheet["date"]}>{item.author}</Text>
       </View>
        <View style={_styleSheet["article"]}>
          <RichText nodes={item.content} />
        </View>

        <View style={_styleSheet["footer-block"]} />
        <View style={_styleSheet["footer"]}>
          <View style={_styleSheet["read-count"]}>{item.readAmount}次浏览</View>
          <View style={_styleSheet["share"]}>
            <View onClick={this.toggleCollection.bind(this)} style={_styleSheet["collection"]}>
              {!isCollection && <Image src={iconCollection} style={_styleSheet["collection-img"]} />}
              {isCollection && <Image src={iconCollection_on} style={_styleSheet["collection-img"]} />}
              收藏
            </View>
            <View style={_styleSheet["collection"]}>
              <Image src={iconShare} style={_styleSheet["share-img"]} />
              分享
            </View>
          </View>
        </View>

        <AtToast isOpened={isTips} text={tipsText} />
      </View>;
  }
}, _class2.config = {
  navigationBarTitleText: '公告详情'
}, _temp)) || _class) || _class);


export default Detail;